home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / glchess / scene / opengl / texture.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.6 KB  |  144 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from OpenGL.GL import *
  5. from OpenGL.GLU import *
  6. import png
  7. import array
  8.  
  9. class Texture:
  10.     '''
  11.     '''
  12.     __data = None
  13.     __format = GL_RGB
  14.     __width = None
  15.     __height = None
  16.     __ambient = None
  17.     __diffuse = None
  18.     __specular = None
  19.     __emission = None
  20.     __shininess = None
  21.     __texture = None
  22.     
  23.     def __init__(self, fileName, ambient = (0.2, 0.2, 0.2, 1), diffuse = (0.8, 0.8, 0.8, 1), specular = (0, 0, 0, 1), emission = (0, 0, 0, 1), shininess = 0):
  24.         """Constructor for an openGL texture.
  25.         
  26.         'fileName' is the name of the image file to use for the texture (string).
  27.         
  28.         An IOError is raised if the file does not exist.
  29.         This does not need an openGL context.
  30.         """
  31.         self._Texture__ambient = ambient
  32.         self._Texture__diffuse = diffuse
  33.         self._Texture__specular = specular
  34.         self._Texture__emission = emission
  35.         self._Texture__shininess = shininess
  36.         
  37.         try:
  38.             self._Texture__loadPIL(fileName)
  39.         except ImportError:
  40.             self._Texture__loadPNG(fileName)
  41.  
  42.  
  43.     
  44.     def __loadPNG(self, fileName):
  45.         '''
  46.         '''
  47.         
  48.         try:
  49.             reader = png.Reader(fileName)
  50.         except IOError:
  51.             e = None
  52.             print 'Error loading texture file: %s: %s' % (fileName, e.strerror)
  53.             self._Texture__data = None
  54.             return None
  55.  
  56.         
  57.         try:
  58.             (width, height, data, metaData) = reader.read()
  59.         except png.Error:
  60.             e = None
  61.             print 'Error parsing PNG file %s: %s' % (fileName, e.message)
  62.             self._Texture__data = None
  63.             return None
  64.  
  65.         self._Texture__width = width
  66.         self._Texture__height = height
  67.         self._Texture__data = array.array('B', data).tostring()
  68.         if metaData['has_alpha']:
  69.             self._Texture__format = GL_RGBA
  70.         else:
  71.             self._Texture__format = GL_RGB
  72.  
  73.     
  74.     def __loadPIL(self, fileName):
  75.         '''
  76.         '''
  77.         import Image
  78.         
  79.         try:
  80.             image = Image.open(fileName)
  81.         except IOError:
  82.             e = None
  83.             print 'Error loading texture file: %s: %s' % (fileName, e.strerror)
  84.             self._Texture__data = None
  85.             return None
  86.  
  87.         width = image.size[0]
  88.         height = image.size[1]
  89.         w = 1
  90.         while 2 * w <= width:
  91.             w *= 2
  92.         h = 1
  93.         while 2 * h <= height:
  94.             h *= 2
  95.         self._Texture__width = w
  96.         self._Texture__height = h
  97.         image = image.crop((0, 0, w, h))
  98.         self._Texture__data = image.tostring('raw', 'RGB', 0, -1)
  99.         self._Texture__format = GL_RGB
  100.  
  101.     
  102.     def __generate(self):
  103.         '''
  104.         '''
  105.         if self._Texture__data is None:
  106.             return 0
  107.         texture = glGenTextures(1)
  108.         glBindTexture(GL_TEXTURE_2D, texture)
  109.         glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
  110.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
  111.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
  112.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
  113.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
  114.         
  115.         try:
  116.             gluBuild2DMipmaps(GL_TEXTURE_2D, GL_LUMINANCE, self._Texture__width, self._Texture__height, self._Texture__format, GL_UNSIGNED_BYTE, self._Texture__data)
  117.         except GLUerror:
  118.             self._Texture__data is None
  119.             e = self._Texture__data is None
  120.             glTexImage2D(GL_TEXTURE_2D, 0, 3, self._Texture__width, self._Texture__height, 0, self._Texture__format, GL_UNSIGNED_BYTE, self._Texture__data)
  121.         except:
  122.             self._Texture__data is None
  123.  
  124.         return texture
  125.  
  126.     
  127.     def bind(self):
  128.         '''Bind this texture to the current surface.
  129.         
  130.         This requires an openGL context.
  131.         '''
  132.         if self._Texture__texture is None:
  133.             self._Texture__texture = self._Texture__generate()
  134.             self._Texture__data = None
  135.         
  136.         glBindTexture(GL_TEXTURE_2D, self._Texture__texture)
  137.         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, self._Texture__ambient)
  138.         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, self._Texture__diffuse)
  139.         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, self._Texture__specular)
  140.         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, self._Texture__emission)
  141.         glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, self._Texture__shininess)
  142.  
  143.  
  144.